#!/usr/bin/env python

# -*- coding: utf-8 -*

"""
蜂鸣器
"""

from py_code.driver.Pin import Pin
from py_code.SingletonType import SingletonType
from py_code.driver.GpioDriver import GpioDriver
import RPi.GPIO as GPIO
import logging
import time


class Buzzer(GpioDriver, metaclass = SingletonType):
    """蜂鸣器"""

    pins = [Pin(32, GPIO.OUT, description = "蜂鸣器引脚")]

    def ring(self, t: float = 0.5, frequency: int = 440) -> None:
        """
        蜂鸣器发出声音
        @param t 持续时间
        @param frequency 频率
        """
        p = GPIO.PWM(self.pins[0].id, frequency)
        # 占空比，0-100 一般设置50，波形则是等宽高低电平
        logging.debug(f"蜂鸣器驱动：发出声音，持续时间：{t}，频率：{frequency}")
        p.start(50) 
        time.sleep(t)
        p.stop()
